#include "menu.h"

void start_menu_game()
{
	//keyboard interruption
	_go32_dpmi_seginfo old_kbd_irq;
	mask_pic(KBD_IRQ);	
	install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
	unmask_pic(KBD_IRQ);
	queueInit(&keys_queue);
	
	load_char_table(table);

	
	//menu sprite declarations
	menu1 = create_unbuffered_sprite_from_file("menu1.xpm");
	menu2 = create_unbuffered_sprite_from_file("menu2.xpm");
	menu3 = create_unbuffered_sprite_from_file("menu3.xpm");
	//help = create_unbuffered_sprite_from_file("up1.xpm");
	
	song_init("mm1bomb.txt");
	
	menu_game();

	song_finalize();
	
	//delete sprites and keyboard reinstallation
	reinstall_asm_irq_handler(KBD_IRQ, &old_kbd_irq);
	/*
	destroy_sprite(menu1);
	destroy_sprite(menu2);
	destroy_sprite(menu3);
	*/
}


void menu_game() {
	draw_sprite_scaled(menu1, base, HRES, VRES);
	
	
	
	
	int currentState = 1;

	//States: 1->newgame 2->help 3->exit
	while (1) {
	
		scancode = queueGet(&keys_queue);
		switch (currentState) {

		/*-------NEWGAME-------*/
		case 1:
			switch (scancode) {
			case 0://escape
				return;
			case 72://up
				delete_sprite_scaled(menu1, base, HRES, VRES);
				draw_sprite_scaled(menu3, base, HRES, VRES);
				currentState = 3;
				break;
			case 80://down
				delete_sprite_scaled(menu1, base, HRES, VRES);
				draw_sprite_scaled(menu2, base, HRES, VRES);
				currentState = 2;
				break;
			case 28://enter
				delete_sprite_scaled(menu1, base, HRES, VRES);
				clear_screen(0,base);
				newGame();
				draw_sprite_scaled(menu1, base, HRES, VRES);
				break;
			default:
				break;
			}
			break;
			
		/*-------HELP-------*/
		case 2:
			switch (scancode) {
			case 0://escape
				return;
			case 72://up
				delete_sprite_scaled(menu2, base, HRES, VRES);
				draw_sprite_scaled(menu1, base, HRES, VRES);
				currentState = 1;
				break;
			case 80://down
				delete_sprite_scaled(menu2, base, HRES, VRES);
				draw_sprite_scaled(menu3, base, HRES, VRES);
				currentState = 3;
				break;
			case 28://enter
				delete_sprite_scaled(menu2, base, HRES, VRES);
				help();
				draw_sprite_scaled(menu2, base, HRES, VRES);
				break;
			default:
				break;
			}
			break;
			
		/*-------EXIT-------*/
		default:
			switch (scancode) {
			case 0://escape
				return;
			case 72://up
				delete_sprite_scaled(menu3, base, HRES, VRES);
				draw_sprite_scaled(menu2, base, HRES, VRES);
				currentState = 2;
				break;
			case 80://down
				delete_sprite_scaled(menu3, base, HRES, VRES);
				draw_sprite_scaled(menu1, base, HRES, VRES);
				currentState = 1;
				break;
			case 28://enter
				return;
			default:
				break;
			}
			break;
		}

	}
	return;
}














void menu_test_graphics() {


	mouse_init();
	
	while (1) {
	
		scancode = queueGet(&keys_queue);
		
		if(!(isEmptyGQueue(mouse_queue)))
		{
			parse_mouse_event();
			process_mouse_event_gfx();
		}
	
		switch (scancode) {
		case 1://escape
			mouse_finalize();
			return;
		}
}
	
	
	

	return;
}

void help()
{
clear_screen(0,base);
drawDialog("INSTRUCOES","Teclas direccionais para andar\nEnter para fazer desaparecer o dialogo");
clear_screen(0,base);
drawDialog("INSTRUCOES","Esc para terminar voltar ao menu\nPara falar com um polvo andar em direccao a ele");
}
